package com.wz.wiki.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wz.wiki.entity.pojo.User;
import com.wz.wiki.entity.pojo.UserExample;
import com.wz.wiki.entity.req.UserLoginReq;
import com.wz.wiki.entity.req.UserQueryReq;
import com.wz.wiki.entity.req.UserResetPassWordReq;
import com.wz.wiki.entity.req.UserSaveReq;
import com.wz.wiki.entity.resp.UserLoginResp;
import com.wz.wiki.entity.resp.UserQueryResp;
import com.wz.wiki.entity.resp.PageResp;
import com.wz.wiki.exception.BusinessException;
import com.wz.wiki.exception.BusinessExceptionCode;
import com.wz.wiki.mapper.UserMapper;
import com.wz.wiki.service.UserService;
import com.wz.wiki.utils.CopyBeanUtils;
import com.wz.wiki.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author 王哲
 * @Contact 1121586359@qq.com
 * @ClassName TestService.java
 * @create 2023年06月28日 下午2:53
 * @Description 电子书业务层接口实现类
 * @Version V1.0
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private UserMapper userMapper;

    @Resource
    private SnowFlake snowFlake;

    @Override
    public PageResp<UserQueryResp> selectList(UserQueryReq req) {
        UserExample userExample = new UserExample();
        // 创建查询条件
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getLoginName())){
            criteria.andLoginNameEqualTo(req.getLoginName());
        }

        PageHelper.startPage(req.getPage(), req.getSize());
        List<User> users = userMapper.selectByExample(userExample);

        PageInfo<User> userPageInfo = new PageInfo<>(users);
        System.out.println("总行数：" + userPageInfo.getTotal());

        PageResp<UserQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(userPageInfo.getTotal());
        pageResp.setList(CopyBeanUtils.copyList(users, UserQueryResp.class));

        return pageResp;
    }

    @Override
    public void save(UserSaveReq req) {

        Long id = req.getId();
        User user = CopyBeanUtils.copy(req, User.class);
        if (id == null) {
            if (!Objects.isNull(selectByLoginName(req.getLoginName()))){
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
            // 新增
            id = snowFlake.nextId();
            user.setId(id);
            userMapper.insert(user);
        }else {
            // 更新
            user.setLoginName(null);
            user.setPassword(null);
            userMapper.updateByPrimaryKeySelective(user);
        }
    }

    @Override
    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void resetPassword(UserResetPassWordReq req) {
        User user = CopyBeanUtils.copy(req, User.class);
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public UserLoginResp login(UserLoginReq req) {
        User user = selectByLoginName(req.getLoginName());
        if (ObjectUtils.isEmpty(user)) {
            // 用户名不存在
            LOG.error("用户名不存在, {}", req.getLoginName());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }
        if (!user.getPassword().equals(req.getPassword())) {
            // 密码不正确
            LOG.error("密码不正确, 输入密码：{}, 数据库密码：{}", req.getPassword(), user.getPassword());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }
        return CopyBeanUtils.copy(user, UserLoginResp.class);
    }


    private User selectByLoginName(String loginName){
        UserExample userExample = new UserExample();
        // 创建查询条件
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(loginName);
        List<User> users = userMapper.selectByExample(userExample);

        if (CollectionUtils.isEmpty(users)){
            return null;
        }

        return users.get(0);
    }
}
